//
//  NSString+Extension.m
//  包子微博
//
//  Created by 尚承教育 on 15/6/7.
//  Copyright (c) 2015年 魔力包. All rights reserved.
//

#import "NSString+Extension.h"
#import <CommonCrypto/CommonCrypto.h>
@implementation NSString (Extension)

+ (CGSize)sizeWithString:(NSString *)string fontSize:(CGFloat)fontSize maxWidth:(CGFloat)maxWidth maxHeight:(CGFloat)maxHeight
{
    
    CGSize maxSize = CGSizeMake(maxWidth, maxHeight);
    NSDictionary *stringDict = @{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]};
    return [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:stringDict context:nil].size;
}

- (CGSize)sizeWithfont:(UIFont *)font maxWidth:(CGFloat)maxWidth maxHeight:(CGFloat)maxHeight
{
    CGSize maxSize = CGSizeMake(maxWidth, maxHeight);
    NSDictionary *stringDict = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:stringDict context:nil].size;
}

+(NSString*)encodeString:(NSString*)unencodedString{
    
    // CharactersToBeEscaped = @":/?&=;+!@#$()~',*";
    // CharactersToLeaveUnescaped = @"[].";
    
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)unencodedString,
                                                              NULL,
                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                              kCFStringEncodingUTF8));
    return encodedString;
}

//URLDEcode
-(NSString *)decodeString:(NSString*)encodedString

{
    //NSString *decodedString = [encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ];
    
    NSString *decodedString  = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
                                                                                                                     (__bridge CFStringRef)encodedString,
                                                                                                                     CFSTR(""),
                                                                                                                     CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    return decodedString;
}


- (NSString *)length6
{
    if (self.length >= 6) {
        NSString *str = [self substringFromIndex:self.length-6];
        return str;
    }else{
        return [NSString stringWithFormat:@"%6ld",(long)self.integerValue];
    }
}
- (NSString *)img//图片地址
{
    return [NSString stringWithFormat:@"%@/file/images/%@",Base_URL,self];
}
- (NSString *)MD5
{
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), digest ); // This is the md5 call
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  [output lowercaseString];
}
-(NSString *)cutStr {
    if (self.length > 10) {
        NSString *str1 = [self substringToIndex:5];
        NSString *str2 = [self substringFromIndex:self.length - 6];
        NSString *string = [str1 stringByAppendingString:[NSString stringWithFormat:@"****%@",str2]];
        return string;
    }
    return self;
}
-(NSString *)cutStrWithLength:(NSInteger)length {
    if (self.length > length) {
        NSString *str = [self substringToIndex:length];
        NSString *string = [str stringByAppendingString:@"..."];
        return string;
    }
    return self;
}
#pragma mark - 将某个时间转化成 时间戳
+ (NSString *)getTimeStrWithString:(NSString *)str{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"YYYY-MM-dd"]; //设定时间的格式
    NSDate *tempDate = [dateFormatter dateFromString:str];//将字符串转换为时间对象
    NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[tempDate timeIntervalSince1970]*1000];//字符串转成时间戳,精确到毫秒*1000
    return timeStr;
}

@end
